// import adminUserApi from '@/api/adminUser'
import { getToken, setToken, removeToken, getTenantId } from '@/utils/auth'
import avatar from '@/assets/img/avatar.png'
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: avatar,
    roles: [],
    factory: {},
    tenantId: getTenantId()
  },

  mutations: {
    SET_TENANTID: (state, tenantId) => {
      state.tenantId = tenantId
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_FACTORY: (state, factory) => {
      state.factory = factory
    }
  },

  actions: {
    // 登录
    Login ({ commit }, data) {
      return new Promise((resolve, reject) => {
        setToken(data)
        // setTenantId(data.tenantId)
        commit('SET_TOKEN', data)
        // commit('SET_TENANTID', data.tenantId)
        resolve()
      })
    },

    // 获取用户信息
    GetInfo ({ commit, state }, { roles, name, avatar, factory }) {
      return new Promise((resolve, reject) => {
        commit('SET_ROLES', roles)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar || state.avatar)
        commit('SET_FACTORY', factory)
        resolve()
      })
    },

    // 登出
    LogOut ({ commit, state }) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resolve()
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 前端 登出
    FedLogOut ({ commit, dispatch }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        dispatch('clearWxInfo')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
